from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time
from selenium.webdriver.support.ui import Select
from untitled1.auto_test.cz_yl import res1
from untitled1.auto_test.cz_sj import res_l
import random
from selenium.webdriver.common.keys import Keys


class AutoInit(object):

    def __init__(self):
        # 设置初始条件
        self.dz = r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
        self.url = r'http://192.168.0.150:8082/ciis_dp/'
        binary = FirefoxBinary(self.dz)
        browser = webdriver.Firefox(firefox_binary=binary)
        self.browser = browser

    def login_set(self):
        # 登录
        try:
            self.browser.get(self.url)
            self.browser.implicitly_wait(10)
            self.browser.find_element_by_id('username').send_keys('xfdj')
            self.browser.find_element_by_id('password').send_keys('666666')
            # 判断是否有验证码，如果有，则让用户输入。
            try:
                self.browser.find_element_by_id('code')
                # js = 'var code = prompt("验证码");return code;'
                # code = self.browser.execute_script(js)
                # js2 = 'var t = "123";return t'
                # code2 = self.browser.execute_script(js2)
                time.sleep(8)
            except:
                self.browser.find_element_by_class_name('btn_login').click()
            self.browser.find_element_by_class_name('btn_login').click()
        except:
            pass

    def recover_init(self):
        # 循环前的初始化
        self.browser.implicitly_wait(10)
        try:
            self.browser.find_element_by_xpath("//img[@alt='事件分拨']").click()
            self.browser.find_element_by_id('btn_control').click()
            self.browser.find_element_by_id('div_6').click()
            time.sleep(1)
            self.browser.find_element_by_id('li_6_0').click()
            time.sleep(1)
            # self.browser.switch_to.default_content()
            self.browser.switch_to.frame('rightFrame')
        except:
            exit()

    def recover_action(self, value, test_c):
        # 新增页面
        self.browser.find_element_by_xpath("//div[@class='gn_from']/span[6]").click()
        self.browser.implicitly_wait(10)
        # self.browser.switch_to.frame('rightFrame')
        temp_count = 0
        for i in value:
            # 如果值里面，长度大于2的，是input，等于2的是select
            if len(i) > 2:
                # 如果是text文本，输入值
                if i[1] == 'text':
                    if i[3] == '':
                        try:
                            self.browser.find_element_by_id(i[0]).send_keys(test_c[temp_count])
                            temp_count += 1
                        except:
                            print(i)
                    else:
                        # 如果是只读属性，通过js去掉只读属性，填写值
                        js = "$('input[id=%s]').removeAttr('readonly')" % i[0]
                        self.browser.execute_script(js)
                        try:
                            self.browser.find_element_by_id(i[0]).send_keys(test_c[temp_count])
                            temp_count += 1
                        except:
                            print(i)
                # 如果是radio，点击
                elif i[1] == 'radio':
                    # 此处为处理方式选择
                    if i[2] == 'dealtype':
                        if i[0] == 'dealtype4':
                            self.browser.find_element_by_id('dealtype4').click()
                        else:
                            continue
                    else:
                        # 如果id是等于这个的，就按下翻页键
                        if i[0] == 'warninglv2':
                            # js = r"$('.dengji').offset({top:-400,left:0})"
                            # self.browser.execute_script(js)
                            self.browser.find_element_by_id(i[0]).send_keys(Keys.PAGE_DOWN)
                            try:
                                self.browser.find_element_by_id(i[0]).click()
                            except:
                                print(i)
                        else:
                            try:
                                self.browser.find_element_by_id(i[0]).click()
                            except:
                                print(i)
                else:
                    try:
                        # 其它的是文本域，输入值
                        self.browser.find_element_by_id(i[0]).send_keys(test_c[temp_count])
                        temp_count += 1
                    except:
                        print(i)
            else:
                try:
                    # 选择下拉框，长度大于1的才选，否则的话跳过。
                    temp = Select(self.browser.find_element_by_id(i[0])).options
                    if len(temp) > 1:
                        Select(self.browser.find_element_by_id(i[0])).select_by_index(random.randint(1, len(temp)-1))
                    else:
                        print(i)
                except:
                    print(i)
        # 提交
        self.browser.find_element_by_id('submitBtn').click()
        # try:
        #     # 如果有弹窗，点击确定
        #     self.browser.switch_to.alert.accept()
        # except:
        #     pass

    def recover_end(self):
        try:
            self.browser.quit()
        except:
            pass


t = AutoInit()
t.login_set()
t.recover_init()
for i in range(1):
    try:
        t.recover_action(res1(), res_l()[i])
    except:
        t.recover_init()

